<template>

</template>

<script>
import { uploadPic, saveDoctorUserPictureUrl } from 'api/userInfo'

export default {
  name: 'minixUploadPic',
  data() {
    return {
      uploadMaxSize: 2000 * 1024,
      czzyShow: false,
      czzyImages: [],
      mediaType: 'img'
    }
  },
  methods: {
    preview(type) {
      let imgArr, mediaType = 'img'
      if (type == 'zjz') {
        imgArr = [require('../../src/assets/img/zjzCzzy/zjz-czzy1.png'), require('../../src/assets/img/zjzCzzy/zjz-czzy2.png'), require('../../src/assets/img/zjzCzzy/zjz-czzy3.png'), require('../../src/assets/img/zjzCzzy/zjz-czzy4.png')]
      } else if (type == 'ylt') {
        imgArr = [require('../../src/assets/img/ylt.jpg')]
      } else if (type == 'video') {
        imgArr = require('../../src/assets/mp4/zjz_study.mp4')
        mediaType = 'video'
      } else if (type == 'yhs') {
        imgArr = [require('../../src/assets/img/yhsCzzy/yhs1.png'), require('../../src/assets/img/yhsCzzy/yhs1-1.png'), require('../../src/assets/img/yhsCzzy/yhs1-2.png'), require('../../src/assets/img/yhsCzzy/yhs2.png'), require('../../src/assets/img/yhsCzzy/yhs2-2.png'), require('../../src/assets/img/yhsCzzy/yhs2-3.png'), require('../../src/assets/img/yhsCzzy/yhs2-4.png'), require('../../src/assets/img/yhsCzzy/yhs2-5.png'), require('../../src/assets/img/yhsCzzy/yhs2-6.png')]
      } else if (type == 'ykm') {
        imgArr = [require('../../src/assets/img/ykmCzzy/ykm-czzy1.jpg'), require('../../src/assets/img/ykmCzzy/ykm-czzy2.jpg'), require('../../src/assets/img/ykmCzzy/ykm-czzy3.jpg'), require('../../src/assets/img/ykmCzzy/ykm-czzy4.jpg'), require('../../src/assets/img/ykmCzzy/ykm-czzy5.jpg')]
      } else if (type == 'xck') {
        imgArr = [require('../../src/assets/img/xck-czzy.jpg')]
      }
      this.mediaType = mediaType
      this.czzyImages = imgArr
      this.czzyShow = true
    },
    afterRead(uploadType) {
      return file => {
        // 此时可以自行将文件上传至服务器
        let urlJson = this.selfUserInfo[uploadType][0]
        urlJson.status = 'uploading'
        urlJson.message = '上传中...'
        uploadPic(file.file, this.selfUserInfo.idcard, uploadType)
          .then(res => {
            urlJson.url = res.data
            urlJson.status = 'done'
            this.savePicUrl(uploadType, res.data)
            this.$forceUpdate()
            // getPictureList(this.selfUserInfo.id, uploadType)
            //   .then(res => {
            //     //固定路径格式
            //     res = JSON.parse(res.data)
            //     let url = 'https://ggfw.hrss.foshan.gov.cn/fshr/resource/view/' + res[0].resourceid + '/0'
            //     urlJson.status = 'done'
            //   }).catch(err => {
            //   console.log(err)
            // })

          }).catch(err => {
          urlJson.status = 'failed'
          urlJson.message = '上传失败'
          this.selfUserInfo[uploadType].pop()
        })
      }
    },
    deletePic(uploadType) {
      // deletePic(this.selfUserInfo.id, uploadType)
      //   .then(res => {
      //     this.selfUserInfo[uploadType] = []
      //   }).catch(err => {
      //   console.log(err)
      // })
      this.selfUserInfo[uploadType] = []
      this.savePicUrl(uploadType)
    },
    savePicUrl(picType, url) {
      let dbPicUrl = this.selfUserInfo.uploadpicture
      dbPicUrl[picType] = url
      let pic = JSON.stringify(dbPicUrl)
      saveDoctorUserPictureUrl({ id: this.selfUserInfo.id, uploadpicture: pic })
    },
    onOversize() {
      this.$dialog.alert({
        title: '错误信息',
        message: '图片不能超过2Mb'
      })
    }
  }
}
</script>

<style scoped>

</style>
